<template>
  <div class="inventoryPosting">
    <div class="header">
      <div class="header-name">
        <el-date-picker
          v-model="fdate"
          type="daterange"
          align="right"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="mini"
          :picker-options="pickerOptions"
          style="width: 240px"
        />
        <!--        <search-->
        <!--          :options="selectData"-->
        <!--          :search-son="searchSon"-->
        <!--          :msg="tacitly"-->
        <!--          @seek="collect"-->
        <!--          @hand="handleQuerySonClass"-->
        <!--        />-->
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
        <!--        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />-->
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
      >
        <template v-slot:billSlot="col">
          <el-link type="primary" @click="handleInto(col.scope.row)">
            {{ col.scope.row.物料编码 }}
          </el-link>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  queryAvgList
} from '@/api/financial/salesMargin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
// import filtration from '@/components/filtration'
// import jcButton from '@/components/Button'
import picker from '@/components/Search/picker'
export default {
  name: 'OtherDetail',
  components: {
    // search,
    // filtration
  },
  mixins: [picker],
  data() {
    return {
      fdate: [],
      ftype: 33,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.billType'), prop: '单据类型', width: '100' },
        { label: this.$t('market.odd'), prop: '单号', width: '120' },
        { label: this.$t('market.orderDate'), prop: '单据日期', width: '100' },
        { label: this.$t('market.supplier'), prop: '供应商' },
        { label: this.$t('market.lingDept'), prop: '领料部门/归属' },
        { label: this.$t('market.fmaterial'), prop: '物料编码', type: 'bill', width: '120' },
        { label: this.$t('shop.fname2'), prop: '物料名称', width: '100' },
        { label: this.$t('shop.fspecificaTion'), prop: '物料规格', width: '100' },
        { label: this.$t('market.stock2'), prop: 'FNAME', width: '120' },
        { label: this.$t('market.funit'), prop: '单位', width: '60' },
        { label: this.$t('market.fqty'), prop: '数量', width: '80' },
        { label: this.$t('market.priceUnTax'), prop: '不含税单价', width: '80' },
        { label: this.$t('market.notrateamount'), prop: '不含税金额', width: '80' },
        { label: this.$t('market.month2'), prop: '月份', width: '60' },
        { label: this.$t('market.creator'), prop: '创建人' },
        { label: this.$t('market.tabNote'), prop: '表备注' },
        { label: this.$t('market.note2'), prop: '行备注' },
        { label: this.$t('market.system'), prop: '系统' },
        { label: this.$t('market.differen'), prop: '区分' },
        { label: this.$t('market.classification'), prop: '分类' }
      ],
      tableHeader1: []
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryAvgList({
            pageSize: 100000,
            pageNum: 1,
            sd: this.fdate?.[0],
            ed: this.fdate?.[1]
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '部门领料明细表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取财务报表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        sd: this.fdate?.[0],
        ed: this.fdate?.[1],
        pageNum: this.currentPage,
        pageSize: this.size
        // search: this.searCollData.concat(this.details)
      }
      const { data: RES } = await queryAvgList(DATA)
      this.tableData = RES.array
      this.total = RES.total
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    handleInto(ev) {
      this.$router.push({ path: `/detailMateriel/${ev.物料编码}` })
    }
  }
}
</script>
<style lang="scss" scoped>
.inventoryPosting {
  @include listBom;
  padding: 0;
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 15px !important;
    }
  }
}
</style>
